<?php
include './include/common.inc.php';
require '../include/html.class.php';
require '../include/form.class.php';

$html = new html();
//@set_time_limit(600);

switch($action)
{
    case 'index':
        $filesize = $html->index();
	    showmessage('网站首页更新成功！<br />大小：'.$filesize);
		break;

    case 'category':
        if( isset( $dosubmit ) )
		{
			if( !isset( $count ) )
			{
				$cids = array();
				if( !isset( $catids ) || $catids[0] == 0 || $catids[0] == '' )
				{
					foreach( $CATEGORY as $cid=>$v )
					{
						$catids[] = $cid;//将所有栏目ID放到$catids[]数组中
					}
				}
                foreach( $catids as $k => $id )
                {
                    if( $id != '' )
                    {
                        $cids[] = $id;
                    }
                }
				if( $cids )
				{
                    cache_write( 'CACHE_HTML_CIDS', $cids );
					$count = count( $cids );
                    $forward = get_forward( 'html.php' );
					showmessage('开始更新栏目页...', "admin/html.php?action=$action&forward=$forward&pagesize=50&dosubmit=1&count=$count");
				}
				else
				{
					showmessage('更新完成！', "admin/html.php?action=$action");
				}
            }
			else
			{
				$catids = cache_read( 'CACHE_HTML_CIDS' );
				$page = isset( $page ) ? max( intval( $page ), 1 ) : 1;
				if( $page == 1 )
				{
				    $catid = array_shift( $catids );
					cache_write( 'CACHE_HTML_CIDS', $catids );
                }
				$catname = $CATEGORY[$catid]['catname'];
				if( $CATEGORY[$catid]['child'] )
				{
					$pages = 1;
					$html->category( $catid );
				}
				else
				{
					$offset = $pagesize * ( $page - 1 );
					if( $page == 1 )
					{
						$contents = cache_count( "SELECT COUNT(*) AS `count` FROM `".DB_PRE."content` c LEFT JOIN ".DB_PRE."content_rel r ON c.id = r.contentid WHERE r.catid='$catid' AND c.status=99" );
                        $cat_page = intval($CATEGORY[$catid]['cat_page']) == 0 ? 20 : intval($CATEGORY[$catid]['cat_page']);
                        $total = ceil( $contents/intval($cat_page) );
						$pages = ceil( $total/$pagesize );
					}
					$max = min( $offset + $pagesize, $total );
					for( $i = $offset; $i <= $max; $i++ )
					{
						$html->category( $catid, $i );
					}
				}
				if( $pages > $page )
				{
					$page++;
					$percent = round( $max/$total, 2 ) * 100;
					$message = "正在更新 <font color='blue'>$catname</font> 栏目，共需更新 <font color='red'>$total</font> 个网页<br />已更新 <font color='red'>{$max}</font> 个网页（<font color='red'>{$percent}%</font>）";
					$forward = url_par( "catid=$catid&page=$page&pages=$pages&total=$total" );
				}
				elseif( $catids )
				{
					$message = "<font color='blue'>$catname</font> 栏目更新完成！";
					$forward = url_par("catid=0&page=0&pages=0&total=0");
				}
				else
				{
					cache_delete( 'CACHE_HTML_CIDS' );
					$message = "更新完成！";
					$forward = "admin/html.php?action=$action";
				}
				showmessage( $message, $forward );
			}
		}
		else
		{
			include template( 'admin', 'html_category' );
        }
		break;

    case 'show':
        if( isset( $dosubmit ) )
		{
			require_once 'content.class.php';

			$c = new content('category');
			$currentpage = isset( $currentpage ) ? $currentpage : 0;
			$currentpage = max( intval( $currentpage ), 1 );
            $pagesize = 100;
			$offset = $pagesize * ( $currentpage - 1 );
			
		    $where = ' WHERE c.status=99 ';
			$order = 'DESC';
            $left_join = " LEFT JOIN ".DB_PRE."content_rel r ON c.id = r.contentid";

			if( !isset( $first ) && isset( $catids ) && is_array( $catids ) && $catids[0] > 0 )
			{
				cache_write( 'CACHE_HTML_SHOW_CIDS', $catids );
				$catids = implodeids( $catids );
				$where .= " AND r.catid IN($catids)";
				$first = 1;
			}
			elseif( isset( $first ) )
			{
				$catids = cache_read( 'CACHE_HTML_SHOW_CIDS' );
				$catids = implodeids( $catids );
				$where .= " AND r.catid IN($catids)";
			}
			else
			{
				$first = 0;
			}

			if( !isset( $total ) )
			{
				$total = cache_count( "SELECT COUNT(*) AS `count` FROM `".DB_PRE."content` c $left_join $where" );
				$pages = ceil( $total/$pagesize );
				$start = 1;
			}
            $sql = "SELECT
                    c.id as id,
                    c.title as title,
                    c.style as style,
                    c.thumb as thumb,
                    c.link as link,
                    c.username as username,
                    c.module as module,
                    c.catid as cat,
                    c.modelid as modelid,
                    c.addtime as addtime,
                    c.showaddtime as showaddtime,
                    c.updatetime as updatetime,
                    c.commend as commend,
                    c.status as status,
                    c.listorder as listorder,
                    r.cid as cid,
                    ifnull( r.catid, c.catid ) catid
            FROM ".DB_PRE."content c";
			$data = $db->query( "$sql $left_join $where ORDER BY `id` $order LIMIT $offset,$pagesize" );
			foreach( $data as $r )
			{
				$html->show( $r['cid'], $r['catid'], $r['id'] );
			}
			if( $pages > $currentpage )
			{
				$currentpage++;
				$creatednum = $offset + count( $data );
				$percent = round( $creatednum/$total, 2 )*100;
				$message = "共需更新 <font color='red'>$total</font> 条信息<br />已完成 <font color='red'>{$creatednum}</font> 条（<font color='red'>{$percent}%</font>）";
				$forward = $start ? "admin/html.php?action=$action&dosubmit=1&first=$first&pagesize=$pagesize&currentpage=$currentpage&pages=$pages&total=$total" : preg_replace("/&currentpage=([0-9]+)&pages=([0-9]+)&total=([0-9]+)/", "&currentpage=$currentpage&pages=$pages&total=$total", URL);;
			}
			else
			{
				cache_delete( 'CACHE_HTML_SHOW_CIDS' );
				$message = "更新完成！";
				$forward = "admin/html.php?action=$action";
			}
			showmessage( $message, $forward );
		}
		else
		{
			include template( 'admin', 'html_show' );
        }
		break;

    default :
}

?>
